Programming Elixir 1.3: Functional |> Concurrent |> Pragmatic |> Fun by Dave Thomas

Programming Elixir 1.3: Functional |> Concurrent |> Pragmatic |> Fun by Dave Thomas

Author:Dave Thomas [Thomas, Dave]
Language: eng
Format: azw3, pdf
Tags: Pragmatic Bookshelf
Publisher: Pragmatic Bookshelf
Published: 2016-10-24T04:00:00+00:00


And here are the tests for it:

project/4/issues/test/table_formatter_test.exs

​ ​defmodule​ TableFormatterTest ​do​

​ ​use​ ExUnit.Case ​# bring in the test functionality​

​ ​import​ ExUnit.CaptureIO ​# And allow us to capture stuff sent to stdout​

​ alias Issues.TableFormatter, ​as:​ TF

​ ​def​ simple_test_data ​do​

​ [ [ ​c1:​ ​"​​r1 c1"​, ​c2:​ ​"​​r1 c2"​, ​c3:​ ​"​​r1 c3"​, ​c4:​ ​"​​r1+++c4"​ ],

​ [ ​c1:​ ​"​​r2 c1"​, ​c2:​ ​"​​r2 c2"​, ​c3:​ ​"​​r2 c3"​, ​c4:​ ​"​​r2 c4"​ ],

​ [ ​c1:​ ​"​​r3 c1"​, ​c2:​ ​"​​r3 c2"​, ​c3:​ ​"​​r3 c3"​, ​c4:​ ​"​​r3 c4"​ ],

​ [ ​c1:​ ​"​​r4 c1"​, ​c2:​ ​"​​r4++c2"​, ​c3:​ ​"​​r4 c3"​, ​c4:​ ​"​​r4 c4"​ ] ]

​ ​end​

​ ​def​ headers, ​do​: [ ​:c1​, ​:c2​, ​:c4​ ]

​ ​def​ split_with_three_columns,

​ ​do​: TF.split_into_columns(simple_test_data, headers)

​ test ​"​​split_into_columns"​ ​do​

​ columns = split_with_three_columns

​ assert length(columns) == length(headers)

​ assert List.first(columns) == [​"​​r1 c1"​, ​"​​r2 c1"​, ​"​​r3 c1"​, ​"​​r4 c1"​]

​ assert List.last(columns) == [​"​​r1+++c4"​, ​"​​r2 c4"​, ​"​​r3 c4"​, ​"​​r4 c4"​]

​ ​end​

​ test ​"​​column_widths"​ ​do​

​ widths = TF.widths_of(split_with_three_columns)

​ assert widths == [ 5, 6, 7 ]

​ ​end​

​ test ​"​​correct format string returned"​ ​do​

​ assert TF.format_for([9, 10, 11]) == ​"​​~-9s | ~-10s | ~-11s~n"​

​ ​end​

​ test ​"​​Output is correct"​ ​do​

​ result = capture_io ​fn​ ->

​ TF.print_table_for_columns(simple_test_data, headers)

​ ​end​

​ assert result == ​"""​

​ ​ c1 | c2 | c4​

​ ​ ------+--------+--------​

​ ​ r1 c1 | r1 c2 | r1+++c4​

​ ​ r2 c1 | r2 c2 | r2 c4​

​ ​ r3 c1 | r3 c2 | r3 c4​

​ ​ r4 c1 | r4++c2 | r4 c4​

​ ​ """​

​ ​end​

​ ​end​

(Although you can’t see it here, the output we compare against in the last test contains trailing whitespace.)

Rather than clutter the process function in the CLI module with a long module name, I chose to use import to make the print function available without a module qualifier. This goes near the top of cli.ex.

​ ​import​ Issues.TableFormatter, ​only:​ [ ​print_table_for_columns:​ 2 ]

This code also uses a great Elixir testing feature. By importing ExUnit.CaptureIO, we get access to the capture_io function. This runs the code passed to it, but captures anything written to standard output, returning it as a string.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.